package ie.dkit.semanticPhotoWeb.client.panels.photo;

import ie.dkit.semanticPhotoWeb.client.ClientServerConnectorAsync;
import ie.dkit.semanticPhotoWeb.client.dtos.ImageDTO;
import ie.dkit.semanticPhotoWeb.client.panels.CenterPanel;
import ie.dkit.semanticPhotoWeb.client.panels.tag.TagPanel;
import ie.dkit.semanticPhotoWeb.client.tools.Refreshable;
import ie.dkit.semanticPhotoWeb.client.tools.RelationControllerListener;
import ie.dkit.semanticPhotoWeb.client.tools.RelationCreator;

import com.gwtext.client.core.Margins;
import com.gwtext.client.core.RegionPosition;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.layout.BorderLayout;
import com.gwtext.client.widgets.layout.BorderLayoutData;

/**
 * @author Sebastian Ruehl
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
public class PhotoPanel extends Panel implements RelationControllerListener,
		Refreshable {

	protected RelationCreator m_RelationCreator;

	protected static final String RELATION_MODE_ON = "Set in Relation to this photo";

	protected static final String RELATION_MODE_OFF = "Set in Relation";

	protected ImageDTO m_CurrentImageDTO;

	protected ClientServerConnectorAsync m_ClientServerConnectorAsync;

	protected CenterPanel m_CenterPanel;

	private RelationPanel m_RelationPanel;

	private ImagePanel m_ImagePanel;

	private TagPanel m_TagPanel;

	private DetailPanel m_DetailPanel;

	private CommentsPanel m_CommentsPanel;

	protected Button m_createRelationButton;

	public PhotoPanel(ImageDTO imageDTO,
			ClientServerConnectorAsync clientServerConnectorAsync,
			CenterPanel centerPanel) {
		m_CurrentImageDTO = imageDTO;
		m_ClientServerConnectorAsync = clientServerConnectorAsync;
		m_CenterPanel = centerPanel;
		m_RelationPanel = new RelationPanel(clientServerConnectorAsync,
				imageDTO, centerPanel);
		m_RelationCreator = RelationCreator
				.getInstance(clientServerConnectorAsync);
		m_RelationCreator.addRelationControllerListener(this);
		m_ImagePanel = new ImagePanel(this);
		m_TagPanel = new TagPanel(clientServerConnectorAsync, this);
		m_DetailPanel = new DetailPanel(this);
		m_CommentsPanel = new CommentsPanel(clientServerConnectorAsync,
				imageDTO);
		setClosable(true);
		setTitle(m_CurrentImageDTO.imageTitle);
		setLayout(new BorderLayout());
		add(m_ImagePanel, new BorderLayoutData(RegionPosition.CENTER));

		BorderLayoutData tagPanelLayoutData = new BorderLayoutData(
				RegionPosition.NORTH);
		tagPanelLayoutData.setSplit(true);
		tagPanelLayoutData.setMinSize(20);
		tagPanelLayoutData.setMaxSize(400);
		tagPanelLayoutData.setMargins(new Margins(0, 0, 5, 0));
		add(m_TagPanel, tagPanelLayoutData);

		BorderLayoutData relationPanelLayoutData = new BorderLayoutData(
				RegionPosition.WEST);
		relationPanelLayoutData.setSplit(true);
		relationPanelLayoutData.setMinSize(175);
		relationPanelLayoutData.setMaxSize(400);
		relationPanelLayoutData.setMargins(new Margins(0, 0, 5, 0));
		add(m_RelationPanel, relationPanelLayoutData);

		BorderLayoutData detailPanelLayoutData = new BorderLayoutData(
				RegionPosition.EAST);
		detailPanelLayoutData.setSplit(true);
		detailPanelLayoutData.setMinSize(175);
		detailPanelLayoutData.setMaxSize(400);
		detailPanelLayoutData.setMargins(new Margins(0, 0, 5, 0));
		add(m_DetailPanel, detailPanelLayoutData);

		BorderLayoutData commentsPanelLayoutData = new BorderLayoutData(
				RegionPosition.SOUTH);
		commentsPanelLayoutData.setSplit(true);
		commentsPanelLayoutData.setMinSize(100);
		commentsPanelLayoutData.setMaxSize(400);
		commentsPanelLayoutData.setMargins(new Margins(0, 0, 5, 0));
		add(m_CommentsPanel, commentsPanelLayoutData);
	}

	public ImageDTO getCurrentImageDTO() {
		return m_CurrentImageDTO;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.client.tools.Refreshable#refresh()
	 */
	public void refresh() {
		m_RelationPanel.refresh();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.client.tools.RelationControllerListener#relationModeActivated(ie.dkit.semanticPhotoWeb.client.dtos.ImageDTO)
	 */
	public void relationModeActivated(ImageDTO activator) {
		m_createRelationButton.setText(RELATION_MODE_ON);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see ie.dkit.semanticPhotoWeb.client.tools.RelationControllerListener#relationModeDeactivated()
	 */
	public void relationModeDeactivated() {
		m_createRelationButton.setText(RELATION_MODE_OFF);
		m_RelationPanel.refresh();
	}

	/**
	 * Deregister from the Relation Creator Listener
	 * 
	 */
	public void deRegisterRelationCreatorListener() {
		m_RelationCreator.removeRelationControllerListener(this);
	}
}
